<template>
  <div>
    <!-- 需求计划提报(N+1) -->
    <TopFilter :list="topFilterList" ref="top" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="wkcOmMonthPlanQuery"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="(columns) => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="isView" :title="'计划信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="80%">
      <viewModel :rowData="formData" @close="viewcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import moment from 'moment';
import { queryList, exportExcelOem } from '@/api/dealerSale/salesPlan/MonthPlanReport';
import viewModel from './viewModel';
import { region } from '@/api/cms-common/region';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils';
import { getSaleAreaInfo } from '@/api/cms-common/dealerdata/index';
import { dealerCommon } from '@/api/cms-api/origin/commonDealerType'; //渠道类型工具类

export default {
  name: 'MonthPlanQuery',
  mixins: [dictionary, region, dealerCommon],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: { isDealer: 10041002 },
      formData: {},
      showData: {
        currMonth: moment().format('YYYY年MM月'),
      },
      exportLoading: false,
      isDisabled: false,
      isView: false,
      regionList: [],
      areaList: [],
    };
  },
  components: {
    viewModel,
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    // this.init();
    this.getAreaData();
    //初始化省份信息
    this.initProvince();
  },
  methods: {
    async getAreaData() {
      const res = await getSaleAreaInfo(15061013);
      if (res.resultCode === 200) {
        this.areaList = res.data;
        res.data.map((x) => {
          this.topFilterList.find((item) => item.fieldName == 'areaId').itemList.push({ text: x.orgName, value: x.id });
        });
      }
    },
    areaName(value) {
      for (let i in this.areaList) {
        if (this.areaList[i].id === value) {
          return this.areaList[i].orgName;
        }
      }
    },
    //初始化所在省份
    async initProvince() {
      let pl = this.createRegionList();
      this.topFilterList.find((item) => item.fieldName == 'provinceId').itemList = pl;
    },
    //初始化所在市数据
    async initCity(provinceId) {
      //清空市
      this.topFilterList.find((item) => item.fieldName == 'cityId').initialValue = '';
      if (!provinceId || provinceId == null || typeof provinceId == 'undefined') {
        return;
      }
      //取省数据
      let rd = this.getRegionData();
      this.cityData = rd[provinceId].children;
      let cl = this.createSubRegionList(this.cityData);
      this.topFilterList.find((item) => item.fieldName == 'cityId').itemList = cl;
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    //点击查看事件
    viewHandle(row) {
      this.isView = true;
      this.formData = row;
    },
    //关闭侧边栏事件
    viewcloseHandler(val) {
      this.isView = false;
      // if (val) {
      //   // 重新加载
      //   this.fetchParams = { ...this.fetchParams };
      // }
    },
    // 导出
    async exportHandle() {
      await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$refs.top.SUBMIT_FORM();
          this.exportLoading = true;
          exportExcelOem(this.fetchParams)
            .then((response) => {
              exportExcelFile(response.data, 'C端月度计划提报');
              this.exportLoading = false;
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '计划类型',
          fieldName: 'plan1Type',
          placeholder: '请选择',
          itemList: this.createDictList('2405'),
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'planStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2404'),
        },
        {
          type: 'DATE',
          label: '计划年月',
          fieldName: 'planYearAndMonth',
          // initialValue: moment().subtract(-1, 'months').format('YYYY-MM'),
          dateType: 'month',
          clearable: true,
        },
        { type: 'SELECT', label: 'C端战区', fieldName: 'areaId', placeholder: '请选择', itemList: [] },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          itemList: [],
          change: (value) => {
            this.initCity(value);
          },
        },
        {
          type: 'SELECT',
          label: '城市',
          fieldName: 'cityId',
          placeholder: '请选择',
          itemList: [],
          // change: (value) => {
          //   this.initTown(value)
          // },
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入',
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.viewHandle(props.row)}>
                  查看
                </multiuse-button>
              </div>
            );
          },
        },
        {
          title: '月度计划编号',
          dataIndex: 'plan1No',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '计划类型',
          dataIndex: 'plan1Type',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (props) => {
            //编码转汉字
            return <span>{this.createDictText(props.row.plan1Type, '2405')}</span>;
          },
        },
        {
          title: 'C端战区',
          dataIndex: 'areaId',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (props) => {
            return <span>{this.areaName(props.row.areaId)}</span>;
          },
        },
        {
          title: '省份',
          dataIndex: 'provinceName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '城市',
          dataIndex: 'cityName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商代码',
          dataIndex: 'ownerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '状态',
          dataIndex: 'planStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (props) => {
            //编码转汉字
            return <span>{this.createDictText(props.row.planStatus, '2404')}</span>;
          },
        },

        {
          title: '计划年月',
          dataIndex: 'planYearAndMonth',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '总提报数量',
          dataIndex: 'planAllNum',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '指标提报数',
          dataIndex: 'planIndexNum',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '配额提报数',
          dataIndex: 'planQuotaNum',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '创建人',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          filterType: 'input',
          // editType: 'select',
          // editItems: [],
        },
        {
          title: '创建时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
      ];
    },
  },
};
</script>
